/*!
 * 数值输入控件.
 * ecp.qzz.ui.input.updown.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.inumber","qzz.button"], function() {
	
	var qzz = window.qzz;
	qzz.space("ui.input");
	/**
	 * 数值输入控件.
	 */
	qzz.ui.input.updown = qzz.Class(qzz.ui.input.number, {
		/**
		 * 创建.
		 */
	    _create:function(){
		    this.base();
		    this.className = "Updown";
		    this.logInfo("Updown.create");
		    this._button = this.createElement("DIV");
		    this.upBtn = new qzz.ui.button(undefined, {"cssPrefix":this._cssPrefix});
		    this.upBtn.setWidth(20);
		    this.upBtn.addStyle("componet_ui_input_up_common");
		    this.downBtn = new qzz.ui.button(undefined, {"cssPrefix":this._cssPrefix});
		    this.downBtn.setWidth(20);
		    this.downBtn.setTop(10);
		    this.downBtn.addStyle("componet_ui_input_down_common");
		    this.getOption().loop = this.getBooleanValue(this.getOption().loop, false);
		    var _this = this;
		    this._lockLength = 0
		    if(this.getOption().lockLength != null && this.getOption().lockLength !== "") {
		    	this._lockLength = parseInt(this.getOption().lockLength, 10);
		    	
		    }
	    	this.upBtn.bind("onclick", function() {
	    		if(_this.getOption().disabled === true || _this.getOption().readOnly === true) return;
	    		_this.postValue();
	    		var ov = _this.getValue();
	    		_this.upValue();
	    		var nv = _this.getValue();
	    		_this.trigger("onclick",[_this,ov,nv,nv < ov]);
	    		return false;
	    	});
	    	this.downBtn.bind("onclick", function() {
	    		if(_this.getOption().disabled === true || _this.getOption().readOnly === true) return;
	    		_this.postValue();
	    		var ov = _this.getValue();
	    		_this.downValue();
	    		var nv = _this.getValue();
	    		_this.trigger("onclick",[_this,ov,nv,nv > ov]);
	    		return false;
	    	});
	    },
	    /**
	     * 渲染.
	     */
	    _render:function(){
	    	this.base();
	    	this.logInfo("Updown.render");
	    	if(this._value == null || this._value === '') {
	    		this.setValue(0);
	    	}
	    	this.refresh();
	    },
	    /**
	     * 自增函数.
	     */
	    upValue:function() {
	    	var cv = this.getValue();
	    	var ov = cv;
	    	if(cv == null || cv === "") {
	    		cv = 0;
	    	}
	    	if(this.getOption().maxValue == null || cv < this.getOption().maxValue) {
	    		cv++;
	    	} else if(this.getOption().loop && this.getOption().minValue != null) {
	    		cv = this.getOption().minValue;
	    	}
	    	this.setValue(cv);
	    	this.trigger("onchange", [this, ov, cv, cv < ov]);
	    	this.refresh();
	    },
	    /**
	     * 自减函数.
	     */
	    downValue:function() {
	    	var cv = this.getValue();
	    	var ov = cv;
	    	if(cv == null || cv === "") {
	    		cv = 0;
	    	}
	    	if(this.getOption().minValue == null || cv > this.getOption().minValue) {
	    		cv--;
	    	} else if(this.getOption().loop && this.getOption().maxValue != null) {
	    		cv = this.getOption().maxValue;
	    	}
	    	this.trigger("onchange", [this, ov, cv, cv > ov]);
	    	this.setValue(cv);
	    	this.refresh();
	    },
	    /**
	     * 设置最大值.
	     * @param {number} value 值
	     */
	    setMaxValue:function(value) {
	    	this.getOption().maxValue = value;
	    },
	    /**
	     * 设置最小值.
	     * @param {number} value 值
	     */
	    setMinValue:function(value) {
	    	this.getOption().minValue = value;
	    },
	    /**
	     * 刷新.
	     */
	    refresh:function() {
	    	if(this._lockLength > 0) {
	    		var txtLen = (this._text + "").length;
	    		var preStr = "";
	    		for(var i = txtLen; i < this._lockLength; i++) {
	    			preStr += "0";
	    		}
	    		this._text = preStr + this._text;
	    	}
	    	this.base();
	    	this.setStyle(this._button, "componet_ui_input_updown");
	    	this.inputBox.style.width = (this.getOption().width - 21)+"px";
	    	var bh = (this.getOption().height - 20)/2;
	    	this._button.style.top = bh + "px";
	    	this.upBtn.setParent(this._button);
            this.downBtn.setParent(this._button);	    	
	    	this.upBtn.refresh();
	    	this.downBtn.refresh();
	    	this.thisWindow.appendChild(this._button);	 
	    },
	    /**
	     * 滚动.
	     */
	    doMouseWheel:function(ev, scrollValue) {
	    	if(this.getOption().wheel !== false) {
		    	this.postValue();
		    	if(scrollValue > 0) {
		    		this.upValue();
		    	} else {
		    		this.downValue();
		    	}
	    	}
	    },
	    setLockLength:function(value) {
	    	this._lockLength = value;
	    }
	});
	
	/**
	 * 数值微调编辑控件.
	 */
	_bindRenderFunc("qzzupdown", qzz.ui.input.updown);
	return qzz.ui.input.updown;
});